
import Singleton from "../Singleton";
import { DefStringProtocol, INetworkTips, NetCallFunc } from "./NetInterface";
import { NetManager } from "./NetManager";
import { NetNode } from "./NetNode";
import { WebSock } from "./WebSock";



class NetTips implements INetworkTips {
    
    connectTips(isShow: boolean): void {
        // if (isShow) {
        //     this.getLabel().string = "Connecting";
        //     this.getLabel().node.active = true;
        // } else {
        //     this.getLabel().node.active = false;
        // }
    }

    reconnectTips(isShow: boolean): void {
        // if (isShow) {
        //     this.getLabel().string = "Reconnecting";
        //     this.getLabel().node.active = true;
        // } else {
        //     this.getLabel().node.active = false;
        // }
    }

    requestTips(isShow: boolean): void {
        // if (isShow) {
        //     this.getLabel().string = "Requesting";
        //     this.getLabel().node.active = true;
        // } else {
        //     this.getLabel().node.active = false;
        // }
    }
}

export default class NetMgr extends Singleton<NetMgr> {
    private _netNode: NetNode = null;
    
    private _hNetCallBack = null
    private _connectCallBack = null
    isNotInit(){
        return !this._netNode || this._netNode.isClosed()
    }
    
    getNetNode(){
        return this._netNode;
    }
    
    initEvent(url=null, token,connectCallBack) {
        // netload.showLoad()
        this._hNetCallBack = this.netCallBack.bind(this);
        this._connectCallBack = connectCallBack
        const Node = new NetNode();
        this._netNode = Node;
        Node.init(new WebSock(), new DefStringProtocol(), new NetTips());
        NetManager.getInstance().setNetNode(Node);
        this.onConnect(url,token)
        // this.addResponeHandler(NetMsg.MATCH_GET_ROOM_INFO, this._hNetCallBack, this)
                
        // EventMgr.addEventListener(EventMsg.sendEnterRoomInfo,this.sendEnterRoomInfo,this)
    }

    public close(){
        this._netNode.close();
        this._netNode.rejectReconnect()
    }

    public send(buf: any, data = null,force: boolean = false): boolean {
        return this._netNode.send(buf, data, force);
    }

    public demoMessage(cmd, data){
        let json = {
            cmd: cmd,
            status: 0,
            data: data,
        }
        this._netNode.testMessage(json)
    }

    addResponeHandler(cmd: number | string, callback: NetCallFunc, target?: any) {
        this._netNode.addResponeHandler(cmd, callback, target);
    }

    removeResponeHandler(cmd: number | string, callback: NetCallFunc, target?: any) {
        this._netNode.removeResponeHandler(cmd, callback, target);
    }

    public onConnect(url,token): void {
        console.warn("开始连接：" + url)
        let that = this
        let obj = {
            url: url ,
            token: token,
            login:()=>{
                if(that._connectCallBack)that._connectCallBack()
            }
        }
        
        this._netNode.connect(obj);
    }

    netCallBack(cmd, data: any) {
        data = data.data
        switch (Number(cmd)) {
            default:
                break;
        }
    }

    resetDelayMsg(){
        this._netNode.resetDelayMsg()
    }
}
export let netMgr = NetMgr.getInstance();